﻿using System;
using System.Collections.Generic;
using System.Text;
using BCData.CRM.CustomerReceptionRecord;
using BCEntity.CRM.CustomerReceptionRecord;
using BCService.Utils;
using BCData.Common.Area;
using BCCommon;
using BCDto.Sim.Admin.Admin;
using System.Linq;
using BCEntity.Sim.Admin.Admin;
using BCDto.CRM.CustomerReceptionRecord;

namespace BCService.CRM.CustomerReceptionRecord
{
    public class CustomerReceptionRecordService : ICustomerReceptionRecordService
    {
        private ICustomerReceptionRecordData customerReceptionRecordData;
        private IProvinceData provinceData;
        private ICityData cityData;

        public CustomerReceptionRecordService(ICustomerReceptionRecordData customerReceptionRecordData, IProvinceData provinceData, ICityData cityData)
        {
            this.customerReceptionRecordData = customerReceptionRecordData;
            this.provinceData = provinceData;
            this.cityData = cityData;
        }

        #region Insert
        public CustomerReceptionRecordDto Insert(AdminDto admin, CustomerReceptionRecordRequestDto dto)
        {
            var fEntity = dto.As<CustomerReceptionRecordEntity>();
            fEntity.CompanyId = admin.CompanyId;
            fEntity.AdminId = admin.AdminId;
            fEntity.AdminFullName = admin.FullName;
            fEntity.IsArchive = false;
            fEntity.CustomerLevel = BCCommon.CRMEnums.CRMCustomerLevel.A.ToString(); 
            CheckInput(fEntity); 

            return customerReceptionRecordData.InsertAsync(fEntity).GetAwaiter().GetResult().As< CustomerReceptionRecordDto>();
        }
        #endregion

        #region CheckInput
        private void CheckInput(CustomerReceptionRecordEntity ccEntity)
        {
            if (string.IsNullOrWhiteSpace(ccEntity.ProvinceCode))
                throw new ArgumentNullException("省ID不能是空!");
            var fProvince = provinceData.GetProvince(ccEntity.ProvinceCode) ?? throw new ArgumentException("查不到 省名称!");
            ccEntity.ProvinceName = fProvince.ProvinceName;


            if (string.IsNullOrWhiteSpace(ccEntity.CityCode))
                throw new ArgumentException("市ID不能是空!");
            var f1 = cityData.GetCity(ccEntity.CityCode) ?? throw new ArgumentException("查不到市名称!");
            ccEntity.CityName = f1.CityName;
             

            if (string.IsNullOrWhiteSpace(ccEntity.UserName))
                throw new ArgumentException("客户名称不能是空"); 

            if (string.IsNullOrWhiteSpace(ccEntity.Phone))
                throw new ArgumentException("客户电话不能是空!");

            if (string.IsNullOrWhiteSpace(ccEntity.Address))
                throw new ArgumentException("客户地址不能是空!");
        }
        #endregion
         
        #region update
        public CustomerReceptionRecordDto Update(AdminDto admin, CustomerReceptionRecordDto dto)
        {
            var fEntity = dto.As<CustomerReceptionRecordEntity>();
            if (fEntity.CompanyId <= 0)
                fEntity.CompanyId = admin.CompanyId;
            fEntity.AdminId = admin.AdminId;
            fEntity.AdminFullName = admin.FullName;
            CheckInput(fEntity); 

            return customerReceptionRecordData.UpdateAsync( fEntity).GetAwaiter().GetResult().As< CustomerReceptionRecordDto>();
        }
        #endregion

        #region Delete
        public void Delete(long id)
        {
            customerReceptionRecordData.Delete(id).GetAwaiter().GetResult(); 
        }
        #endregion

        #region GetByID
        public CustomerReceptionRecordDto GetByID(long ccID)
        {
            var a = customerReceptionRecordData.GetById(ccID).GetAwaiter().GetResult();
            return a.As<CustomerReceptionRecordDto>();
        }
        #endregion

        #region GetList
        public PageableList<CustomerReceptionRecordDto> GetList(AdminDto ccAdmin,
                DateTime? ccArrivalTime1,
                DateTime? ccArrivalTime2,
                DateTime? ccExitTime1,
                DateTime? ccExitTime2,
                string ccUserName,
                string ccPhone,
                string ccEmail,
                string ccProvinceId,
                string ccCityId,
                string ccCountyId,
                int pageNo ,
                int pageSize )
        {
            Console.WriteLine(string.Format("{0},{1},{2},{3},{4}", ccAdmin, ccArrivalTime1, ccArrivalTime2, ccExitTime1, ccExitTime2));
            var (fCount,fList) = customerReceptionRecordData.GetListAsync(ccAdmin.As<AdminEntity>(), ccArrivalTime1, ccArrivalTime2, ccExitTime1, ccExitTime2,ccAdmin.CompanyId
                ,ccUserName, ccPhone, ccEmail, ccProvinceId, ccCityId, ccCountyId, pageNo, pageSize).GetAwaiter().GetResult();
            return new PageableList<CustomerReceptionRecordDto>()
            {
                Count = fCount,
                Items = fList.As<List<CustomerReceptionRecordDto>>(),
                PageIndex = pageNo,
                PageSize = pageSize,
                ServerTime = DateTime.Now
            };
        }
        #endregion
    }
}
